package com.kindergarten.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kindergarten.pojo.Classes;
import com.kindergarten.pojo.Sign;
import com.kindergarten.service.ISignService;
import com.kindergarten.service.IUserService;
import com.kindergarten.utils.MMGridPageVoBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/emp-sign-api")
public class EmpSignController {

    @Autowired
    ISignService signService;

    @Autowired
    IUserService iUserService;

    /**
     * 01-跳转到模板引擎页面
     *
     * @return
     */
    @GetMapping("topage")
    public String topage() {
        return "empSign";
    }

    /**
     * 02-数据加载分页
     * @return
     */
    @ResponseBody
    @RequestMapping("empsign")
    public MMGridPageVoBean page(Sign sign) {

        MMGridPageVoBean pageVoBean = new MMGridPageVoBean();
        QueryWrapper<Sign> queryWrapper = new QueryWrapper<Sign>();
        queryWrapper.orderByDesc("id");
        queryWrapper.eq("del",0); //删除不查询
        queryWrapper.in("kqr_type",1,2);

        queryWrapper.ge(sign.getStartTime() != null && !"".equals(sign.getStartTime()), "sign_in", sign.getStartTime());
        queryWrapper.le(sign.getEndTime() != null && !"".equals(sign.getEndTime()), "sign_in", sign.getEndTime());
        IPage<Sign> page = signService.page(new Page<Sign>(sign.getPage(), sign.getLimit()), queryWrapper);

        List<Sign> records = page.getRecords();
        for (Sign record : records) {
            record.setUserName(iUserService.getById(record.getKqrId()).getUserName());
        }
        pageVoBean.setRows(page.getRecords()); //每页数据
        pageVoBean.setTotal(page.getTotal()); //总条数
        return pageVoBean;
    }
}
